﻿#include "Player.h"

void Player::sortCards() 
{

	//外层循环 记录当前处理的位置/实际就是为了找到这个位置改放的值
	int idx;
	int len = countCards();
	int temp;
	for (int i = 0; i < len - 1; i++)
	{
		idx = i;
		// 内层循环，用来寻找当前的最大值
		for (int j = i + 1; j < len; j++)
		{
			// 比较 current_max_idx 和 j 索引里值的大小，如果j里的大，则更新current_max_idx

			int p1 = cards[idx] % 13 + 1;
			int p2 = cards[j] % 13 + 1;

			//A
			if (p1 == 1) p1 = 14;
			if (p2 == 1) p2 = 14;
			//2
			if (p1 == 2) p1 = 15;
			if (p2 == 2) p2 = 15;
			//小王
			if (cards[idx] == 52) p1 = 16;
			if (cards[j] == 52) p2 = 16;
			//大王
			if (cards[idx] == 53) p1 = 17;
			if (cards[j] == 53) p2 = 17;
			//♦10
			if (cards[idx] == 22) p1 = 18;
			if (cards[j] == 22) p2 = 18;
			//♥10
			if (cards[idx] == 48) p1 = 19;
			if (cards[j] == 48) p2 = 19;

			if (p1 < p2)
			{
				idx = j;
			}
			else if (p1 == p2)//花色不同的情况
			{
				if (cards[idx] < cards[j])
				{
					idx = j;
				}
			}
		}

		// 如果最大值的索引发生改变，则交换两个位置的值
		if (idx != i)
		{
			int tmp = cards[i];
			cards[i] = cards[idx];
			cards[idx] = tmp;
		}
	}
}

void Player::initCards() 
{
	for (int i = 0; i < 14; i++) 
	{
		cards[i] = -1;
	}
}

int Player::countCards() 
{
	int count = 0;
	for (int i = 0; i < 14; i++) 
	{
		if (cards[i] == -1) 
		{
			break;
		}
		count++;
	}
	return count;
}

// 一个中文，占3个字节
// 一个普通字符，占1个字节
string getAccountWithMaxLength(const char *name, int maxLength) 
{

	string res = ""; // 储存返回的结果
	int currentLength = 0; // 记录的是宽度(并非字符串长度)
	for (int i = 0; i < strlen(name); i++) 
	{

		// 判断当前宽度是否已经到达最大宽度
		if (currentLength == maxLength) 
		{
			res += "..";
			break;
		}

		// 判断当前遍历的字符是否是中文
		// 汉字由8bit组成且最高位为1，通过右移7位然后&1来判断是否为汉字
		if (name[i] >> 7 & 1 && name[i + 1] >> 7 & 1 && name[i + 2] >> 7 & 1) 
		{
			if (currentLength + 2 > maxLength) // 放不下当下遍历的中文，则加点结束
			{ 
				res += "..";
				break;
			}
			else // 放得下，则连续放入3个字符，且宽度+2
			{ 
				currentLength += 2;
				res += name[i++];
				res += name[i++];
				res += name[i];
			}
		}
		else // 非中文，则放入1个字符，且宽度+1
		{ 
			currentLength += 1;
			res += name[i];
		}
	}

	return res;
}

void Player::createPlayerNode() 
{
	this->node = Node::create();

	string res = getAccountWithMaxLength(this->account.c_str(), 8);

	Label *nameLabel = Label::createWithSystemFont(res, "Arial", 25);
	nameLabel->setWidth(120);
	nameLabel->setAlignment(TextHAlignment::CENTER);

	// 安全区域的尺寸（适配全面屏时，记得考虑使用）
//    Director::getInstance()->getSafeAreaRect();

	float labelHalfWidth = nameLabel->getBoundingBox().size.width / 2;
	float labelHalfHeight = nameLabel->getBoundingBox().size.height / 2;

	if (this->pos_char == 's') // 底部玩家
	{ 
		nameLabel->setPosition(Vec2(labelHalfWidth + 30, labelHalfHeight + 30));
	}
	else if (this->pos_char == 'r') 
	{
		nameLabel->setPosition(Vec2(VISIBLE_SIZE.width - 30 - labelHalfWidth, VISIBLE_SIZE.height / 2 + labelHalfHeight));
	}
	else if (this->pos_char == 'f') 
	{
		nameLabel->setPosition(Vec2(VISIBLE_SIZE.width / 2 + labelHalfWidth, VISIBLE_SIZE.height - 200 - labelHalfHeight));
	}
	else if (this->pos_char == 'l') 
	{
		nameLabel->setPosition(Vec2(30 + labelHalfWidth, VISIBLE_SIZE.height / 2 + labelHalfHeight));
	}

	// 模版节点
	Node *stencil = Node::create();
	Sprite *circle = Sprite::create("circleModel.png");
	circle->setScale(120.f / 200); // 尺寸和目标匹配
	stencil->addChild(circle);

	// 设置模版
	ClippingNode *clippingNode = ClippingNode::create(stencil);
	// 创建头像精灵
	Sprite *avatarSprite = Sprite::create("new.png");
	// 设置底板
	clippingNode->addChild(avatarSprite);
	// Alpha阀值：只有模版中像素的alpha值大于这个阀值，内容才会被裁剪
	clippingNode->setAlphaThreshold(0.5);
	// setInverted:
	// false时，默认值、显示模版裁剪下来的底板内容
	// true时，显示剩余部分
	clippingNode->setInverted(false);
	clippingNode->setPosition(Vec2(nameLabel->getPositionX(), nameLabel->getBoundingBox().getMaxY() + avatarSprite->getBoundingBox().size.height / 2 + 10));
	clippingNode->setTag(1);
	// 加一个头像边框
	Sprite *borderSprite = Sprite::create("youxi4.png");
	borderSprite->setPosition(clippingNode->getPosition());


	this->node->addChild(nameLabel);
	this->node->addChild(clippingNode);
	this->node->addChild(borderSprite);
}
void Player::show(int res)
{
	Sprite* sp = Sprite::create(StringUtils::format("show_redten%d.png", res));
	Node* avater_nade = node->getChildByTag(1);
	Rect rect = avater_nade->getBoundingBox();
	sp->setScale(0.5);

	if (pos_char == 's' || pos_char == 'l')
	{
		sp->setPositionX(rect.origin.x + 135.f / 2 + sp->getBoundingBox().size.width / 2);
		sp->setPositionY(rect.origin.x + 135.f / 2 + sp->getBoundingBox().size.height / 2 - 30.f);

	}
	else
	{
		sp->setPositionX(rect.origin.x + 135.f / 2 - sp->getBoundingBox().size.width / 2 + 25.f);
	}

	sp->setPositionY(rect.origin.y + 135.f / 2 - sp->getBoundingBox().size.height / 2);
	sp->setTag(2);

	node->addChild(sp);

}